package com.zabra.zl.ui.login;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.zabra.zl.Config;
import com.zabra.zl.MainActivity;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.LoginBean;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.forgetpwd.ForgetPwdActivity;
import com.zabra.zl.ui.login.contract.LoginContract;
import com.zabra.zl.ui.login.presenter.LoginPresenter;
import com.zabra.zl.ui.regist.RegistActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.SharedPreferencesUtil;
import com.zabra.zl.utils.ToastUtil;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends BaseMvpActivity<LoginPresenter> implements LoginContract.View {


    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.userpwd)
    EditText userpwd;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.bt_login)
    TextView btLogin;
    @BindView(R.id.bt_regist)
    TextView btRegist;
    private String strUsername, strUserpwd;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        mPresenter = new LoginPresenter();
        mPresenter.attachView(this);
        String uname = SharedPreferencesUtil.getInstance(this).getSP("uname");
        if (!TextUtils.isEmpty(uname)) {
            etUsername.setText(uname);
        }
    }


    @OnClick({R.id.tv_forget, R.id.bt_login, R.id.bt_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_forget:
                //忘记密码
                toClass(this, ForgetPwdActivity.class);
                break;
            case R.id.bt_login:
                //登录
                strUsername = etUsername.getText().toString().trim();
                strUserpwd = userpwd.getText().toString().trim();
                if (TextUtils.isEmpty(strUsername) || TextUtils.isEmpty(strUserpwd)) {
                    ToastUtil.showToast(this, "请输入账号和密码");
                } else {
                    keyList.add("telOrName");
                    keyList.add("loginPwd");
                    valueList.add(strUsername);
                    valueList.add(strUserpwd);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                }
                break;
            case R.id.bt_regist:
                //注册
                toClass(this, RegistActivity.class, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 1) {
            etUsername.setText(data.getStringExtra("tel"));
        }
    }

    @Override
    public void onSuccess(LoginBean bean, String token) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            SharedPreferencesUtil.getInstance(this).putSP("token", token);
            SharedPreferencesUtil.getInstance(this).putSP("debtorid", bean.getDebtorId());
            SharedPreferencesUtil.getInstance(this).putSP("uname", strUsername);

            toClass1(this, MainActivity.class);
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }
}
